

/**
 * 2021年7月31日13:48:39
 * 
 */


const express = require('express')
const mysql = require('mysql')

const conn = mysql.createConnection({
  host: 'localhost',
  port: '3306',
  user: 'root',
  password: '',
  database: 'dzrly'
});
const app = express();

app.listen(8888, (req, res) => {
  console.log('8888ok')
})

app.use(express.static('./public'))
app.all('/', (req, res) => {
  res.redirect('/login.html');
})
// app.get('/', function (req, res) {
//   res.sendFile(__dirname + "/" + "login.html");
// })

//使用body-paser将post请求的数据解析为对象
app.use(express.urlencoded({
  extended: false
}))

app.post('/add', (req, res) => {
  console.log(req.body);
  conn.query('insert into emp set ?', [req.body], (err, result) => {
    if (err) throw err;
    console.log(result)
    res.send('ok')
  })
})

//get传参1：
app.get('/check', (req, res) => {
  console.log(req.query);
  conn.query('select * from emp where eid = ?', [req.query.eid], (err, result) => {
    if (err) throw err;
    console.log(result)
    res.send('ok!!!')
  })
})

//get传参2：
app.get('/check2/:eid', (req, res) => {
  console.log(req.params);
  conn.query('select * from emp where eid = ?', [req.params.eid], (err, result) => {
    if (err) throw err;
    console.log(result)
    res.send('ok!!!')
  })
})